// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/daguan/api/DaguanDeviceApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/daguan/model/DaguanDeviceReq'
import OneClass from '@api/daguan/model/DaguanVolumeConfigQueryReq'
import EditClass from '@api/daguan/model/DaguanVolumeConfigReq'
import DelClass from '@api/daguan/model/DaguanDeviceCommonReq'
import BaseClass from '@api/daguan/model/DaguanDeviceDto'
import VolumeClass from '@api/daguan/model/VolumePeriod'

const PassClass = DelClass
const RejClass = DelClass
const SubmitClass = DelClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'dto']
const VOLUME = [VolumeClass, 'config', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'daguanDeviceQuery', DTO)
    return [resp.comQuery.totalCount, resp.dto || [], { 1: resp.online, 3: resp.offline }]
  },
  transList(arr) {
    arr.forEach((item) => {
      item.downloadPercent = item.downloadPercent === '-1' ? '未下载' : `${item.downloadPercent}%`
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ deviceId }, TIP = '查询单项') {
    const DTO = CORE.grpcDTO(OneClass, { deviceId })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryVolumeConfig', DTO)
    return resp
  },
  transOne({ serverFirst = 0, config = [] }, { state: { one } }) {
    const obj = { ...one, serverFirst }
    config.forEach((item, i) => {
      obj[`hour${i + 1}`] = item.start
      obj[`volume${i + 1}`] = item.value
    })
    return obj
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEdit(one) {
    const config = Array(3).fill().map((_, i) => ({ start: one[`hour${i + 1}`], value: one[`volume${i + 1}`] }))
    return { ...one, config }
  },
  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, VOLUME)
    await CORE.grpc(TIP, CLIENT, 'updateVolumeConfig', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '重启') {
    const DTO = CORE.grpcDTO(DelClass, { id })
    await CORE.grpc(TIP, CLIENT, 'reboot', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  async pass({ id }, TIP = '开屏') {
    const DTO = CORE.grpcDTO(PassClass, { id })
    await CORE.grpc(TIP, CLIENT, 'turnOn', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  async rej({ id }, TIP = '关屏') {
    const DTO = CORE.grpcDTO(RejClass, { id })
    await CORE.grpc(TIP, CLIENT, 'shutDown', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit({ id }, TIP = '提交') {
    const DTO = CORE.grpcDTO(SubmitClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'screenShot', DTO)
    if (!resp || !resp.picUrl) throw new Error(`${TIP}：图片为空`)
    return resp
  },
  dealSubmit(resp, { state: { MAP } }) {
    MAP.snap = resp.picUrl
  },
}
